const app = getApp();
const utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [],
    showId: null,
    loadingMoreHidden: true,
    showCompany: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 获取名片列表
  getAiCardList: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/BusinessCard/GetAiCardList?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        that.setData({
          loadingMoreHidden: false
        })
        if (res.data.Code == 0) {
          if (res.data.Data.length == 0) {
            that.setData({ showCompany: true })
          }
          let cardList = res.data.Data;
          if (cardList.length > 0) {
            for (let i = 0; i < cardList.length; i++) {
              cardList[i].Avater = utils.formatAvater(cardList[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
            }
          }
          that.setData({
            cardList: cardList
          })
        }
      },
    })
  },

  //名片操作
  updateDisplayState: function (e) {
    var that = this;
    let id = e.currentTarget.dataset.id;
    let cardList = that.data.cardList;
    for (let i = 0; i < cardList.length; i++) {
      if (cardList[i].Id == id) {
        var state = cardList[i].DisplayState;
        if (state == 1) {
          state = 2;
          wx.removeStorageSync("cardId")
        } else {
          state = 1;
          wx.setStorageSync("cardId", id)
        }
      }
    }
    wx.request({
      url: app.globalData.host + '/api/BusinessCard/UpdateDisplayState?uid=' + app.globalData.userInfo.Uid + '&staffId=' + id + '&state=' + state,
      success: function (res) {
        if (res.data.Code == 0) {
          that.getAiCardList();
          that.setData({
            showId: null
          })
        }
      },
    })
  },

  showCancel: function (e) {
    let showid = e.currentTarget.dataset.showid;
    if (this.data.showId == showid) {
      this.setData({
        showId: null
      })
    } else {
      this.setData({
        showId: showid
      })
    }
  },

  tocardDetails: function (e) {
    let cardId = e.currentTarget.dataset.id;
    wx.reLaunch({
      url: '/pages/index/index',
    })
    wx.setStorageSync("cardId", cardId);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (app.globalData.userInfo) {
      console.log("用户信息存在")
      that.getAiCardList();
    } else {
      console.log("用户信息不存在,延时加载")
      setTimeout(function () {
        this.onShow();
      }.bind(that), 1000)
    }
  }

})